package com.abhimanyu.dataaccess;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.abhimanyu.dataaccess.model.DollarRate;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Transaction;

/**
 * @author Abhimanyu
 * 
 */

public class GoogleDatastoreDollarRateDao extends
		AbstractGoogleDatastoreDao<DollarRate> {

	private String dollarRateColumnName;
	private String dateColumnName;
	private String serviceNameColumnName;

	public GoogleDatastoreDollarRateDao() {
		/*
		 * LocalServiceTestHelper helper = new LocalServiceTestHelper( new
		 * LocalDatastoreServiceTestConfig()); helper.setUp();
		 */
		/*
		 * DollarRate dollarRate = new DollarRate(); dollarRate.setDate(new
		 * Date()); dollarRate.setDollarRate("50.00");
		 * dollarRate.setServiceName("Xoom"); save(dollarRate);
		 * 
		 * dollarRate = new DollarRate(); dollarRate.setDate(new Date());
		 * dollarRate.setDollarRate("51.00"); dollarRate.setServiceName("Xoom");
		 * save(dollarRate);
		 */
	}

	public void save(DollarRate dollarRate) {

		Entity dollarRateEntity = new Entity(getTableName());
		Transaction transaction = getDatastoreService().beginTransaction();
		dollarRateEntity.setProperty(getDollarRateColumnName(),
				dollarRate.getDollarRate());
		dollarRateEntity.setProperty(getDateColumnName(), dollarRate.getDate());
		dollarRateEntity.setProperty(getServiceNameColumnName(),
				dollarRate.getServiceName());		
		getDatastoreService().put(dollarRateEntity);
		transaction.commit();
	}

	@Override
	public List<DollarRate> fetchAll() {
		List<DollarRate> listOfDollarRates = null;
		DollarRate rate = null;
		Query query = new Query(getTableName()).addSort(getDateColumnName(), SortDirection.ASCENDING);
		PreparedQuery preparedQuery = getDatastoreService().prepare(query);

		for (Entity dollarRateEntity : preparedQuery.asIterable()) {
			if (listOfDollarRates == null) {
				listOfDollarRates = new ArrayList<DollarRate>();
			}

			rate = constructDollarRate(dollarRateEntity);
			listOfDollarRates.add(rate);
		}
		return listOfDollarRates;
	}

	DateFormat format = new SimpleDateFormat();

	private DollarRate constructDollarRate(Entity dollarRateEntity) {
		DollarRate dollarRate = new DollarRate();
		System.out
				.println("dollarRateEntity.hasProperty(getDateColumnName() : " + getDateColumnName() + " - "
						+ dollarRateEntity.hasProperty(getDateColumnName()));
		System.out
				.println("dollarRateEntity.hasProperty(getDateColumnName() - value : "
						+ dollarRateEntity.getProperty(getDateColumnName()));
		if (dollarRateEntity.hasProperty(getDateColumnName())) {
			dollarRate.setDate((Date) dollarRateEntity
					.getProperty(getDateColumnName()));
		}
		System.out
				.println("dollarRateEntity.hasProperty(getDollarRateColumnName() : " + getDollarRateColumnName() + " - "
						+ dollarRateEntity
								.hasProperty(getDollarRateColumnName()));
		System.out
				.println("dollarRateEntity.hasProperty(getDollarRateColumnName() - value : "
						+ dollarRateEntity
								.getProperty(getDollarRateColumnName()));
		if (dollarRateEntity.hasProperty(getDollarRateColumnName())) {
			dollarRate.setDollarRate(((String) dollarRateEntity
					.getProperty(getDollarRateColumnName())));
		}
		System.out
				.println("ddollarRateEntity.hasProperty(getServiceNameColumnName() : " + getServiceNameColumnName() + " - "
						+ dollarRateEntity
								.hasProperty(getServiceNameColumnName()));
		System.out
				.println("ddollarRateEntity.hasProperty(getServiceNameColumnName() - value: "
						+ dollarRateEntity
								.getProperty(getServiceNameColumnName()));
		if (dollarRateEntity.hasProperty(getServiceNameColumnName())) {
			dollarRate.setServiceName((String) dollarRateEntity
					.getProperty(getServiceNameColumnName()));
		}

		return dollarRate;
	}

	@Override
	public void update(DollarRate object) {
	/*	Query query = new Query(getTableName()).addFilter(getServiceNameColumnName(), FilterOperator.EQUAL, "Xoom");
		PreparedQuery preparedQuery = getDatastoreService().prepare(query);

		for (Entity dollarRateEntity : preparedQuery.asIterable()) {
			System.out.println(dollarRateEntity.getProperties());
			dollarRateEntity.setProperty(getServiceNameColumnName(), "Xoom Corporation");
			getDatastoreService().put(dollarRateEntity);
		}*/
	}

	public String getDollarRateColumnName() {
		return dollarRateColumnName;
	}

	public void setDollarRateColumnName(String dollarRateColumnName) {
		this.dollarRateColumnName = dollarRateColumnName;
	}

	public String getDateColumnName() {
		return dateColumnName;
	}

	public void setDateColumnName(String dateColumnName) {
		this.dateColumnName = dateColumnName;
	}

	public String getServiceNameColumnName() {
		return serviceNameColumnName;
	}

	public void setServiceNameColumnName(String serviceNameColumnName) {
		this.serviceNameColumnName = serviceNameColumnName;
	}

}
